package com.aperos.webservice;

import java.io.IOException;

import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@SuppressWarnings("serial")
public class AddBarServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(AddBarServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	try {
	    	boolean flag = true;
	    	resp.setContentType("text/plain");
	
	    	String name = req.getParameter("name");
	    	if (name == null || "".equals(name)) {
	    		resp.getWriter().println("failed|Name is required");
	    		flag = false;
	    	} else {
	    		name = req.getParameter("name").replace("\n", ", ");
		    	name = name.replace("|", " ");
	    	}
	    	
	    	String address = req.getParameter("address");
	    	if (address == null || "".equals(address)) {
	    		address = null;
	    	} else {
	    		address = req.getParameter("address").replace("\n", ", ");
		    	address = address.replace("|", " ");
	    	}
	    	
	    	String priceBeer = req.getParameter("priceBeer");
	    	Double barPriceBeer = null;
	    	if (priceBeer == null || "".equals(priceBeer)) {
	    		resp.getWriter().println("failed|Beer price is required");
	    		flag = false;
	    	} else {
	    		barPriceBeer = Double.valueOf(priceBeer).doubleValue();
	    	}
	    	
	    	String priceCocktail = req.getParameter("priceCocktail");
	    	Double barPriceCocktail = null;
	    	if (priceCocktail != null && !"".equals(priceCocktail)) {
	    		barPriceCocktail = Double.valueOf(priceCocktail).doubleValue();
	    	} else {
	    		barPriceCocktail = null;
	    	}
	    	
	    	String happyHourBeginHour = req.getParameter("happyHourBeginHour");
	    	Integer barHappyHourBeginHour = null;
	    	if (happyHourBeginHour != null && !"".equals(happyHourBeginHour)) {
	    		barHappyHourBeginHour = Integer.valueOf(happyHourBeginHour).intValue();
	    	} else {
	    		barHappyHourBeginHour = null;
	    	}
	    	
	    	String happyHourBeginMinute = req.getParameter("happyHourBeginMinute");
	    	Integer barHappyHourBeginMinute = null;
	    	if (happyHourBeginMinute != null && !"".equals(happyHourBeginMinute)) {
	    		barHappyHourBeginMinute = Integer.valueOf(happyHourBeginMinute).intValue();
	    	} else {
	    		barHappyHourBeginMinute = null;
	    	}
	    	
	    	String happyHourEndHour = req.getParameter("happyHourEndHour");
	    	Integer barHappyHourEndHour = null;
	    	if (happyHourEndHour != null && !"".equals(happyHourEndHour)) {
	    		barHappyHourEndHour = Integer.valueOf(happyHourEndHour).intValue();
	    	} else {
	    		barHappyHourEndHour = null;
	    	}
	    	
	    	String happyHourEndMinute = req.getParameter("happyHourEndMinute");
	    	Integer barHappyHourEndMinute = null;
	    	if (happyHourEndMinute != null && !"".equals(happyHourEndMinute)) {
	    		barHappyHourEndMinute = Integer.valueOf(happyHourEndMinute).intValue();
	    	} else {
	    		barHappyHourEndMinute = null;
	    	}
	    	
	    	
	    	String priceBeerHappyHour = req.getParameter("priceBeerHappyHour");
	    	Double barPriceBeerHappyHour = null;
	    	if (priceBeerHappyHour != null && !"".equals(priceBeerHappyHour)) {
	    		barPriceBeerHappyHour = Double.valueOf(priceBeerHappyHour).doubleValue();
	    	} else {
	    		barPriceBeerHappyHour = null;
	    	}
	    	
	    	
	    	String priceCocktailHappyHour = req.getParameter("priceCocktailHappyHour");
	    	Double barPriceCocktailHappyHour = null;
	    	if (priceCocktailHappyHour != null && !"".equals(priceCocktailHappyHour)) {
	    		barPriceCocktailHappyHour = Double.valueOf(priceCocktailHappyHour).doubleValue();
	    	} else {
	    		barPriceCocktailHappyHour = null;
	    	}
	    	
	    	String latitudeE6 = req.getParameter("latitudeE6");
	    	Integer barLatitudeE6 = null;
	    	if (latitudeE6 == null || "".equals(latitudeE6)) {
	    		resp.getWriter().println("failed|Latitude is required");
	    		flag = false;
	    	} else {
	    		barLatitudeE6 = Integer.valueOf(latitudeE6).intValue();
	    	}
	    	
	    	String longitudeE6 = req.getParameter("longitudeE6");
	    	Integer barLongitudeE6 = null;
	    	if (longitudeE6 == null || "".equals(longitudeE6)) {
	    		resp.getWriter().println("failed|Longitude is required");
	    		flag = false;
	    	} else {
	    		barLongitudeE6 = Integer.valueOf(longitudeE6).intValue();
	    	}
	    	
	    	String money = req.getParameter("money");
	    	if (money == null || "".equals(money)) {
	    		money = "€";
	    	}
	
	    	if (flag) {
		    	Bar bar = new Bar(barLatitudeE6, barLongitudeE6, name, address, barPriceBeer, barPriceCocktail, 
		    			barHappyHourBeginHour, barHappyHourBeginMinute, barHappyHourEndHour, barHappyHourEndMinute, 
		    			barPriceBeerHappyHour, barPriceCocktailHappyHour, money, null);
		
		        PersistenceManager pm = PMF.get().getPersistenceManager();
		        try {
		            pm.makePersistent(bar);
		            resp.getWriter().println("OK");
		        } finally {
		            pm.close();
		        }
		        
		        Properties props = new Properties();
		        Session session = Session.getDefaultInstance(props, null);

		        String msgBody = "Un bar a été ajouté :\n";
		        msgBody += "id ->" + String.valueOf(bar.getKey().getId()) + "\n";
		        msgBody += "nom -> " + bar.getName() + "\n";
		        msgBody += "adresse -> " + bar.getAddress() + "\n";
		        msgBody += "prix de la pinte -> " + bar.getPriceBeer() + "\n";

		        try {
		            MimeMessage msg = new MimeMessage(session);
		            msg.setFrom(new InternetAddress("aperospaschers@gmail.com", "Apéros pas chers", "UTF-8"));
		            msg.addRecipient(Message.RecipientType.TO,
		            		new InternetAddress("aperospaschers@gmail.com", "Apéros pas chers", "UTF-8"));
		            msg.addRecipient(Message.RecipientType.TO,
                            new InternetAddress("victor.bonnet@gmail.com", "Victor", "UTF-8"));
		            msg.setSubject("Bar ajouté : " + bar.getName(), "UTF-8");
		            msg.setText(msgBody);
		            Transport.send(msg);

		        } catch (AddressException e) {
		            e.printStackTrace();
		        } catch (MessagingException e) {
		            e.printStackTrace();
		        }
		        
		        
	    	} else {
	    		resp.getWriter().println("failed");
	    	}
    	} catch (Exception e) {
			e.printStackTrace();
			resp.getWriter().println("failed|Exception");
		}
    }
}